/**
 * package smb.
 */
package smb;

import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import robocode.ScannedRobotEvent;

/**
 * my competitive robot version 2.
 * 
 * @author Shoji
 * 
 */
public class PwnU2 extends Robot {
 private boolean target = false;
 private int count = 0;

 /**
  * run the robot.
  * 
  */
 public void run() {

  // turnRadarRight(360);
  while (true) {
   counterWise();
  }
 }

 /**
  * moves to the upper right corner of map and travels along its walls in a
  * counter clockwise fashion.
  */
 public void counterWise() {

  turnRight(90 - getHeading());
  ahead(getBattleFieldWidth() - (getX()));

  turnRadarRight(360);
  // resume();
  turnLeft(90);
  ahead(getBattleFieldHeight() - (getY()));

  turnRadarRight(360);
  // resume();
  turnLeft(90);
  ahead(getBattleFieldWidth());

  turnRadarRight(360);
  // resume();
  turnLeft(90);
  ahead(getBattleFieldHeight());

  turnRadarRight(360);
  // resume();
  turnLeft(90);
  ahead(getBattleFieldWidth());

  turnRadarRight(360);
  // resume();
  turnLeft(90);
  ahead(getBattleFieldHeight());

 }

 /**
  * Depending on distance, power of bullet is adjusted accordingly.
  * 
  * @param e
  *         - see doc below
  * @see robocode.Robot#onScannedRobot(robocode.ScannedRobotEvent)
  */
 public void onScannedRobot(ScannedRobotEvent e) {
  if (!target) {
   target = true;
  }

  // //////////////////////////////////////////////
  // /used for debug purposes
  // /////////////////////////////////////////////
  double targetDistance = e.getDistance();
  this.setDebugProperty("targetDistance", Double.toString(targetDistance));

  if (target) {
   if (e.getDistance() > 400) {

    turnGunRight(normalRelativeAngleDegrees(e.getBearing()
      + (getHeading() - getGunHeading())));
    fire(1);
    scan();
   }
   else if (e.getDistance() < 399 && e.getDistance() > 200) {

    turnGunRight(normalRelativeAngleDegrees(e.getBearing()
      + (getHeading() - getGunHeading())));
    fire(2);
    scan();
   }
   else if (e.getDistance() < 199) {

    turnGunRight(normalRelativeAngleDegrees(e.getBearing()
      + (getHeading() - getGunHeading())));
    fire(3);
    scan();
   }
   if (getEnergy() < 20) {
    turnGunRight(normalRelativeAngleDegrees(e.getBearing()
      + (getHeading() - getGunHeading())));
    fire(1);
    scan();
   }
   resume();
  }
 }

 /**
  * If hit, run to center of the map (non-Javadoc)
  * does not move until it robot is hit 3 times.
  * 
  * @param e - see doc below
  * @see robocode.Robot#onHitByBullet(robocode.HitByBulletEvent)
  */
 public void onHitByBullet(HitByBulletEvent e) {
  double xCen = getBattleFieldWidth() / 2 - (getX());
  double yCen = getBattleFieldHeight() / 2 - (getY());

  // //////////////////////////////////////////////
  // /used for debug purposes
  // could not get to work properly. trying again next time
  // /////////////////////////////////////////////
  // this.setDebugProperty("xCen", Double.toString(xCen));
  // this.setDebugProperty("yCen", Double.toString(yCen));
  count++;
  if (count == 2) {
   turnRight(90 - getHeading());
   ahead(xCen);
   turnLeft(90);
   ahead(yCen);
   count = 0;
  }
  
 }

/**
 *  /**
  * if hit a robot, get bearing, and reverse. 
  * will also get the robot hit bearing and point gun
  * in turn, will shoot robot according to distance.
  * 
@param e - see doc below
  * @see robocode.Robot#onHitRobot(robocode.HitRobotEvent)
 */
 public void onHitRobot(HitRobotEvent e) {

  turnRight(e.getBearing() - getHeading());
  turnGunRight(normalRelativeAngleDegrees(e.getBearing()
    + (getHeading() - getGunHeading())));
  back(100);

  this.setDebugProperty("onHit", "true");
 }

}
